package com.lengmou.service.impl;

import com.lengmou.dao.GoodsDao;
import com.lengmou.entity.Goods;
import com.lengmou.service.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Service("GoodsService")
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsDao goodsDao;
    @Override
    public List<Goods> getAllGoods() {
        return goodsDao.querysAll();
    }

    @Override
    public Goods getGoodsById(int id) {
        return goodsDao.queryByid(id);
    }

    @Override
    public List<Goods> getGoodsByNames(String name) {
        return goodsDao.querysByName(name);
    }

    @Override
    public List<Goods> getGoodsByType(int type) {
        return goodsDao.querysByType(type);
    }

    @Override
    public List<Goods> searchGoods(Map<String, Object> map) {

        return goodsDao.search(map);
    }

    @Override
    public int addGoods(Goods goods) {
        Goods goods1 = goodsDao.queryByGoodsName(goods.getName());
        if(goods1==null){
            return goodsDao.addGoods(goods);
        }
//        表示已经存在相同名称
        return -1;
    }

    @Override
    public int setGoodsName(Goods goods) {
        Goods goods1 = goodsDao.queryByGoodsName(goods.getName());
        if(goods1==null){
            return goodsDao.update(goods);
        }
//        表示已经存在相同名称
        return -1;
    }

    @Override
    public int setGoodsPrice(Goods goods) {
        Goods goods1 = goodsDao.queryByid(goods.getId());
        if(goods1!=null){
            if(goods.getPrice()>0){
                goods1.setPrice(goods.getPrice());
                return goodsDao.update(goods1);
            }
//            表示价格不能设定小于0
            return -2;
        }
        return -1;
    }

    @Override
    public int setGoodsState(Goods goods) {
        Goods goods1 = goodsDao.queryByid(goods.getId());
        if(goods1!=null){
            goods1.setState(goods.getState());
            return goodsDao.update(goods1);
        }
        return -1;
    }

    @Override
    public int setGoodsImg(Goods goods) {
        Goods goods1 = goodsDao.queryByid(goods.getId());
        if(goods1!=null){
            goods1.setImg(goods.getImg());
            return goodsDao.update(goods1);
        }
        return -1;
    }

    @Override
    public int setGoodsType(Goods goods) {
        Goods goods1 = goodsDao.queryByid(goods.getId());
        if(goods1!=null){
            goods1.setType(goods.getType());
            return goodsDao.update(goods1);
        }
        return -1;
    }

    @Override
    public int deleteGoods(int id) {
        Goods goods1 = goodsDao.queryByid(id);
        if(goods1!=null){
            return goodsDao.deleteGoods(id);
        }
        return -1;
    }

    @Override
    public int updataGoods(Goods goods) {
        return goodsDao.update(goods);
    }
}
